package org.fing.edu.uy.esb.adaptative.ws.egov.dgi;

import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
import javax.xml.ws.Endpoint;
import javax.xml.ws.soap.Addressing;

import org.fing.edu.uy.esb.adapative.ws.testing.PrintName;
import org.fing.edu.uy.esb.adaptative.ws.utils.WsOptionProces;

@Addressing()
@WebService(serviceName="DGIWS")
@SOAPBinding(parameterStyle=ParameterStyle.WRAPPED, style=Style.DOCUMENT, use=Use.LITERAL)
public class DGIWS {

	private Endpoint endpoint;
	private Endpoint endpoint2;
	private final int version = 1;
	
	@WebMethod(exclude=true)
	public void publish(int port , String wsName){
		try{
			InetAddress address = InetAddress.getLocalHost();			
			String url = "http://" + address.getHostName() + ":" + port + "/" + wsName;
			endpoint = Endpoint.publish(url, this);		
			System.out.println("Service v"+ version +" published at:" + url);
			url = "http://localhost:" + port + "/" + wsName;
			endpoint2 = Endpoint.publish(url, this);		
			System.out.println("Service v"+ version +" published at:" + url);
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Exception in publish " + this.getClass().getSimpleName(), ex);
		}
	}
	
	@WebMethod(exclude=true)
	public void unpublish(){
		try{
			if(endpoint!=null && endpoint.isPublished()){
				endpoint.stop();
				endpoint2.stop();
				System.out.println("Service stopped");
			}
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Exception in unpublish " + this.getClass().getSimpleName(), ex);
		}
	}
	
	
	
	/**
	 * 
	 * Es la misma que la del BPSWS.
	 * 
	 * @param emp
	 * @return RUT de la empresa generada
	 */
	@WebMethod()
	public String registrarNuevaEmpresa(EmpresaDGI emp){	
		
		PrintName.printMethodName();
		
		System.out.println("AlDiaBPS = " + emp.isAlDiaBPS());
		System.out.println("AlDiaDGI = " + emp.isAlDiaDGI());
		System.out.println("Ciudad = " + emp.getCiudad());
		System.out.println("Departamento = " + emp.getDepartamento());
		System.out.println("Direccion = " + emp.getDireccion());
		System.out.println("Empleados = " + emp.getEmpleados());
		System.out.println("RUT =" + emp.getRUT());
		
		
		String result = "empresa registrada (DGI)";
		WsOptionProces.processDGI();
		return result;
	}
	
	
	/**
	 * 
	 * @param RUT identificador de la empresa
	 * @return Objeto que representa la empresa con el rut, null en otro caso
	 */
	@WebMethod()
	public EmpresaDGI obtenerInfoEmpresa(String RUT){
		PrintName.printMethodName();
		
		EmpresaDGI empresa = new EmpresaDGI();
		empresa.setAlDiaBPS(true);
		empresa.setAlDiaDGI(true);
		empresa.setCiudad("ciudad (DGI)");
		empresa.setDepartamento("departamento (DGI)");
		empresa.setDireccion("direccion (DGI)");
		empresa.setEmpleados(5000);
		empresa.setRUT(RUT + "(DGI)");
		
		WsOptionProces.processDGI();
		return empresa;
	}	
		
	
	/**
	 * 
	 * @param CI = [ ].[   ].[   ]-[ ]
	 * @return retorna un objeto que representa a la persona.
	 */
	@WebMethod()
	public PersonaDGI obtenerPersona(String cedula){
		PrintName.printMethodName();
		
		PersonaDGI persona = null;
		
		Pattern patronCedula = Pattern.compile("^(\\d)\\.(\\d\\d\\d)\\.(\\d\\d\\d)-(\\d)");
		Matcher matcherCedula = patronCedula.matcher(cedula);
		if (matcherCedula.find()) {
			persona = new PersonaDGI();
			persona.setApellidoMaterno("apellidoMaterno (DGI)");
			persona.setApellidoPaterno("apellidoPaterno (DGI)");
			persona.setCI(cedula);
			persona.setFirmaCedula("");
			persona.setPrimerNombre("primerNombre (DGI)");
			persona.setSegundoNombre("segundoNombre (DGI)");
			persona.setSexo("sexo (DGI)");	
		}
		WsOptionProces.processDGI();
		return persona;
	}
	
}
